package com.example.service;

import com.alipay.easysdk.factory.Factory;
import com.alipay.easysdk.payment.page.models.AlipayTradePagePayResponse;
import com.example.dao.UserInfoDao;
import com.example.exception.RepeateException;
import com.example.model.pojo.UserInfo;
import com.example.model.vo.UVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.UUID;

@Transactional
@Service
public class UserInfoService implements IUserInfoService {

    @Autowired
    UserInfoDao userInfoDao;
    @Value("${alipay.returnUrl}")
    private String returnUrl;


    @Override
    public UVo ulogin(String nickname) {
        if (userInfoDao.getByNickname(nickname) == null) {
            return null;
        }
        return new UVo(nickname, userInfoDao.getByNickname(nickname).getPassword());

    }

    @Override
    public String openVip(Integer uuid)  {
        UserInfo userInfo=userInfoDao.getByUuid(uuid);
        String isvip=userInfo.getIsvip();
        if(isvip.equals("0")) {
            AlipayTradePagePayResponse response = null;
            try {
                response = Factory.Payment.Page()
                        .pay("牛码vip会员", UUID.randomUUID().toString().substring(0,5)+uuid.toString(), "10", returnUrl);
                userInfo.setIsvip("1");
                userInfoDao.save(userInfo);
            } catch (Exception e) {
                throw  new RepeateException();
            }
            return response.body;
        }
        else
        {
            return "error";
        }
    }

}
